/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUITipoVeiculo.java
 *
 * Created on 23/11/2011, 15:48:07
 */

package GUI;


import tp1POO.Persistencia.GerenciadorObjetos;
import tp1POO.Modelo.*;
import javax.swing.JOptionPane;
import tp1POO.GestorLog;
import tp1POO.Verifica;
/**
 *
 * @author fernando
 */
public class GUITipoVeiculo extends javax.swing.JFrame {

    private String msg;

    /** Creates new form GUITipoVeiculo */
    public GUITipoVeiculo() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")




    private boolean erroTipo(){
         boolean erro=false;
         this.msg = "";

        erro = !Verifica.tipoLocacao(campoTipo.getText(), campoTaxaBase.getText(),campoPreçoKm.getText()).isEmpty();
        if (erro) {
            msg = Verifica.tipoLocacao(campoTipo.getText(), campoTaxaBase.getText(),campoPreçoKm.getText());
            JOptionPane.showMessageDialog(jPTipo,msg, "Error", JOptionPane.ERROR_MESSAGE);
        }

        return erro;
    }





    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPTipo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        campoTipo = new javax.swing.JTextField();
        campoTaxaBase = new javax.swing.JTextField();
        campoPreçoKm = new javax.swing.JTextField();
        botOk = new javax.swing.JButton();
        botCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Cadastrar Tipo de Veículo");

        jPTipo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados do Tipo de Veículo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12))); // NOI18N

        jLabel1.setText("TIpo:");

        jLabel2.setText("Taxa Base:");

        jLabel3.setText("Preço por Km:");

        javax.swing.GroupLayout jPTipoLayout = new javax.swing.GroupLayout(jPTipo);
        jPTipo.setLayout(jPTipoLayout);
        jPTipoLayout.setHorizontalGroup(
            jPTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPTipoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPTipoLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(3, 3, 3))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPTipoLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)
                        .addGap(1, 1, 1)))
                .addGroup(jPTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPTipoLayout.createSequentialGroup()
                        .addComponent(campoTaxaBase, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoPreçoKm, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                    .addComponent(campoTipo, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPTipoLayout.setVerticalGroup(
            jPTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPTipoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPTipoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoTaxaBase, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoPreçoKm, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        botOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/certo.gif"))); // NOI18N
        botOk.setText("OK");
        botOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botOkActionPerformed(evt);
            }
        });

        botCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        botCancel.setText("Fechar");
        botCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botOk, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(botCancel))
                    .addComponent(jPTipo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botCancel, botOk});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botOk)
                    .addComponent(botCancel))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botCancel, botOk});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botCancelActionPerformed
        GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Adicionar tipo de veículo foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
        GuiCreator.obterInstancia().removerGuiTipo();
    }//GEN-LAST:event_botCancelActionPerformed

    private void botOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botOkActionPerformed
        Tipo tipo = null;
        String tipoVeiculo = null;
        float taxaBase;
        float precoKm;

        if(!erroTipo()){
            tipoVeiculo = campoTipo.getText();
            taxaBase = Float.parseFloat(campoTaxaBase.getText());
            precoKm = Float.parseFloat(campoPreçoKm.getText());
            tipo = new Tipo(tipoVeiculo,taxaBase,precoKm);
            GerenciadorObjetos.obterInstancia().salvarObj(tipo);
            campoPreçoKm.setText("");
            campoTaxaBase.setText("");
            campoTipo.setText("");
            GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+" - Tipo de veículo cadastrado com sucesso!\n\tNome : "+
                    tipo.getNome()+"\n\tTaxa Base : "+tipo.getTaxaBase()+"\n\tPreço por Km : "+tipo.getPrecoQuilometro());
            JOptionPane.showMessageDialog(jPTipo,"Tipo de Locação cadastrado com Sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_botOkActionPerformed















    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUITipoVeiculo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botCancel;
    private javax.swing.JButton botOk;
    private javax.swing.JTextField campoPreçoKm;
    private javax.swing.JTextField campoTaxaBase;
    private javax.swing.JTextField campoTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPTipo;
    // End of variables declaration//GEN-END:variables

}
